﻿/*!
 * 远光ECP平台3.4版本 
 * 
 * 可见窗口基类.：qzz.ui.Window
 * ecp.qzz.ui.codeview.js
 * @requires qzz.ui
 */

/**
 *可见控件命名空间
 *@name qzz.ui
 *@namespace qzz.ui
 */
 define(["ecp.utils","qzz.window","qzz.copy"], function(utils) {
	
	var qzz = window.qzz;
	
	qzz.space("ui");
	
	/**
	 @description 可见控件基类 
	<pre> 
	此类作为可见控件的基类来应用。
	</pre>
	@class qzz.ui.Window 可见控件基类
	@extends qzz.BaseObject
	@property
	@example
	@author qinzhenzhou@ygsoft.com
	@version 1.0
	@date 2014-03-01
	*/	
	qzz.ui.codeview = qzz.Class(qzz.ui.window, 
	/**@lends qzz.ui.Window# */	
	{
	    /**
	     * 对象创建函数.
	     */
	    _create:function() {
            this.className = "CodeView";
            this.logInfo("CodeView.create");
            this.base();
            if(this.getOption().top == null || this.getOption().top === 0) {
            	this.getOption().top = 4;
            }
            this._rowSplit = "\n";
	    },
	    /**
	     * 渲染函数.
	     */
	    _render:function() {
	    	this.base();
	    	if(this.getOption().code == null && this.getOption().type != "HTML" && this.getBrowser("msie")) {
            	this.getOption().code = this.thisWindow.innerText;            	
            } else if(this.getOption().code == null && this.thisWindow.innerHTML != null) {
            	this.getOption().code = this.thisWindow.innerHTML;            	
            }
	    	this.thisWindow.style.backgroundColor = "#FFFFFF";
	    	this._setHtml(this.thisWindow, "", false);
	    	//复制按钮渲染
	    	this._copyBtn = this.createElement("DIV");
	    	this._copyBtn.id = "copy_btn"; 	 
	    	this._copyBtn.setAttribute("title","复制");
	    	this.setStyle(this._copyBtn, "copyDiv");	    	
	    	var _this = this;
	    	this.thisWindow.appendChild(this._copyBtn);
	    	ZeroClipboard.setMoviePath('/grm/ecp/webcore/components/qzz/ZeroClipboard.swf');  //和html不在同一目录需设置setMoviePath
	        var clip = new ZeroClipboard.Client();   //创建新的Zero Clipboard对象	        
	        clip.addEventListener('mouseOver', function(client) {
	        	var str = _this.getOption().code;
	        	if(str != null) {
	        		str = str.replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/&#37;/g, "%");
	        	}
	        	clip.setText(str);
	        });
	        clip.addEventListener('complete', function(client, text) {     //复制完成后的监听事件
	            utils.notify("复制完成。");   
	            //clip.hide();                                          // 复制一次后，hide()使复制按钮失效，防止重复计算使用次数
	        });	        
	        
	        clip.setHandCursor(true);      //设置鼠标移到复制框时的形状
	        clip.glue("copy_btn", this._copyBtn);
	    	//
            this._list = this._splitRow();
	    	this._codeTable = this.createElement("OL");
	    	this._codeTable.setAttribute("start", 1);
	    	this.setStyle(this._codeTable, "coreIndex");
	    	this.thisWindow.appendChild(this._codeTable);
	    	this.refresh();
	    },	    
	    _splitRow:function() {
	    	var str = this.getOption().code;
	    	if(str == null) {
	    		return [];
	    	} else {
	    	    return str.split(/\n/g); 
	    	}
	    },
	    setCode:function(code){
	    	this.getOption().code = code;
	    	this._splitRow();
	    	this.refresh();
	    },
	    _addRow:function(index, str) {
	    	var li = this.createElement("li");
	    	this._codeTable.appendChild(li);
	    	var k = 0;
	    	//序号行
	    	var span = this.createElement("span");
	    	li.appendChild(span);
	    	var css;
	    	if(index % 2 == 1) {
				css = "code_1";
			} else {
				css = "code_2";
			}
	    	this.setStyle(li, css);
	    	var explain = "";
	    	if(str != null) {
	    		var strAry = str.split("//");
	    		str = strAry[0];
	    	    str = "&nbsp;" + str.replace(/ /g,"&nbsp;").
	    	            replace(/	/g,"&nbsp;&nbsp;&nbsp;&nbsp;");
	    	    //if(this.getOption().type=="HTML") {
	    	    	str = str.replace(/</g,"&lt").replace(/>/g,"&gt");
//	    	    } else {
//	    	    	str = str.replace(/<[^<>]+>/g,
//	    		    	    function(key){
//	    		    	    	return key.replace(/&nbsp;/g," ");
//	    		    });
//	    	    }
	    	    str = str.replace(/(['][^']*[']|["][^"]*["])/g,"<font color='blue'>$1</font>");
		    	str = str.replace(/\b(if|else|function|null|this|var|true|false|return|for|while|in)\b/g,"<strong style='color:#A349A4'>$1</strong>");
		    	if(strAry.length > 1) {
		    		strAry[0] = "//";
		    		explain = "<font style='color:green'>" + strAry.join("") + "</font>";
		    	}
	    	}
	    	
	    	this._setHtml(span, str + explain, false);
	    	
	    	li = null;
	    	span = null;
	    },
	    /**
	     * 控制选择事件函数.
	     */
	    _onselectstart:function(e) {
	    	var ent = e || window.event;
            var tg = ent.target || ent.srcElement;
            var cansel = false;
            if(!this.shiftKeyDown) {	            
	            var cansel = tg.tagName == 'INPUT' || tg.tagName == "TEXTAREA";		            
            }	
            ent.returnValue = cansel;
            return cansel;
	    },
	    refresh:function() {
	    	this._setHtml(this._codeTable, "", false);
	    	if(this._list[0] === "") {
	    		this._list.splice(0,1);
	    	}
	    	var row = this._list.length;
	    	for(var i = 0; i < row; i++) {
	    		this._addRow(i + 1, this._list[i]);
	    	}
	    	//this.thisWindow.style.width = "100%";	
	    	//var ch = this._codeTable.offsetHeight;
	    	//if(ch == 0) {
	    	//	ch = row * 18;
	    	//}
	    	//this.setHeight(ch + 2);
	    	this.thisWindow.style.width = "100%";
	    	this.thisWindow.style.height = "100%";
	    },
//	    doShow:function() {
//	    	var ch = this._codeTable.offsetHeight;
//	    	this.setHeight(ch + 2);
//	    },
		/**
		 * 窗口变化.
		 */
		doResize:function() {
			this.thisWindow.style.width = "100%";	
			this.setHeight(this._codeTable.offsetHeight + 2);
			this.thisWindow.style.width = "100%";
		}
	});
	
	/**
	 * 表格控件.
	 */
	_bindRenderFunc("qzzcodeview", qzz.ui.codeview);
	return qzz.ui.codeview;
});